package com.hejun.bos.action.base;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.hejun.bos.common.base.BaseAction;
import com.hejun.bos.domain.base.Area;
import com.hejun.bos.service.base.AreaService;
import com.hejun.bos.utils.PinYin4jUtils;

@Controller
@Scope("prototype")
@Namespace("/area")
@ParentPackage("json-default")
public class AreaAction extends BaseAction<Area> {
	@Autowired
	private AreaService areaService;

	private File file;

	public void setFile(File file) {
		this.file = file;
	}

	/**
	 * 批量导入
	 * 
	 * @return
	 * @throws IOException
	 * @throws FileNotFoundException
	 */
	@Action(value = "batchImport")
	public String batchImport() throws FileNotFoundException, IOException {
		List<Area> list=new ArrayList<>();
		// 生成工作簿对象,HSSFWorkbook只能用来解析.xls文件
		HSSFWorkbook hssfWorkbook = new HSSFWorkbook(new FileInputStream(file));
		// 得到第一页的数据
		HSSFSheet sheet = hssfWorkbook.getSheetAt(0);
		// 遍历得到每一行的数据
		for (Row row : sheet) {
			if (row.getRowNum() == 0) {
				continue;
			}
			if (row.getCell(0) == null || StringUtils.isBlank(row.getCell(0).getStringCellValue())) {
				continue;
			}
			Area area=new Area();
			area.setId(row.getCell(0).getStringCellValue());
			area.setProvince(row.getCell(1).getStringCellValue());
			area.setCity(row.getCell(2).getStringCellValue());
			area.setDistrict(row.getCell(3).getStringCellValue());
			area.setPostcode(row.getCell(4).getStringCellValue());
			//获得简码
			String shortcode=area.getProvince()+area.getCity()+area.getDistrict();
			String[] array=PinYin4jUtils.getHeadByString(shortcode);
			StringBuffer buffer=new StringBuffer();
			for (String str : array) {
				buffer.append(str);
			}
			shortcode=buffer.toString();
			area.setShortcode(shortcode);
			//获得城市编码
			String citycode=PinYin4jUtils.hanziToPinyin(area.getCity());
			area.setCitycode(citycode);
			list.add(area);
		}
		areaService.save(list);
		return NONE;
	}
	
	@Action(value="findByPage",results={@Result(name="success",type="json")})
	public String findByPage(){
		Pageable pageable=new PageRequest(page-1, rows);
		//构造条件
		Specification<Area> specification=new Specification<Area>() {
			List<Predicate> list=new ArrayList<>();
			@Override
			public Predicate toPredicate(Root<Area> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				if(StringUtils.isNotBlank(model.getProvince())){
					Predicate p1 = cb.like(root.get("province").as(String.class), "%"+model.getProvince()+"%");
					list.add(p1);
				}
				if(StringUtils.isNotBlank(model.getCity())){
					Predicate p2 = cb.like(root.get("city").as(String.class), "%"+model.getCity()+"%");
					list.add(p2);
				}
				if(StringUtils.isNotBlank(model.getDistrict())){
					Predicate p3 = cb.like(root.get("district").as(String.class), "%"+model.getDistrict()+"%");
					list.add(p3);
				}
				return cb.and(list.toArray(new Predicate[0]));
			}
		};
		Page<Area> result=areaService.findAll(specification,pageable);
		changeResultToJson(result);
		return SUCCESS;
	}
	

}
